// pages/addeditAccount/addeditAccount.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //  accountType: ['银行卡', '支付宝', '微信'],
    // accountType: ['银行卡'],
    // accountTypeI: 0,
    username: '',
    openingBank: [],
    openingBankI: 0,
    bankcard: '',
    // p_sn:'',
    phonenum: '',
    idcard: '',
    openingaddress: '',
    agree: false,
    agreeShow: false,
    change: '',
    bank_id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.getBankInfo();
    if (options.type == 1) {
      // 编辑
      wx.setNavigationBarTitle({
        title: '编辑银行卡'
      })
      console.log(options.bank_id);
      this.setData({
        change: 2,
        bank_id: options.bank_id
      })
      this.findBank(options.bank_id);
    } else if (options.type == 2) {
      // 添加
      wx.setNavigationBarTitle({
        title: '添加银行卡'
      })
      this.setData({
        change: 1
      })

    }
  },

  // 选择账号类型
  accountTypeChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      accountTypeI: e.detail.value
    })
  },

  // 选择开户行
  openingBankChange: function (e) {
    console.log(e.detail.value)
    this.setData({
      openingBankI: e.detail.value
    })
  },

  // 服务协议
  agreement: function () {
    this.setData({
      agreeShow: true
    })
    console.log(11)
  },

  //同意协议
  selectagree: function () {
    this.setData({
      agree: !this.data.agree
    })
  },

  // 同意服务条款
  agreeclause: function () {
    this.setData({
      agreeShow: false,
      agree: true
    })
  },

  // 确认
  formSubmit: function (e) {

    if (this.data.agree == false) {
      wx.showToast({
        title: '请同意服务协议',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    // if (e.detail.value.accountType == 0) {
      this.setData({
        username: e.detail.value.username,
        openingBankI: e.detail.value.openingBank,
        bankcard: e.detail.value.bankcard,
        phonenum: e.detail.value.phonenum,
        idcard: e.detail.value.idcard,
        openingaddress: e.detail.value.openingaddress
      })
    // } else {
    //   this.setData({
    //     username: e.detail.value.username,
    //     bankcard: e.detail.value.bankcard,
    //     phonenum: e.detail.value.phonenum,

    //   })
    // }
    if (e.detail.value.change == 1) {
      this.addBankInfo();
    } else if (e.detail.value.change == 2) {
      this.editBank();
    }
  },
  //银行列表
  getBankInfo() {
    var that = this;

    app.ajax({
      url: "Partner/Bank/getBanks",
      data: {

      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            openingBank: res.data.data
          })
        }
      }
    })
  },
  //确认添加银行
  addBankInfo() {
    var that = this;
    // var type = parseInt(that.data.accountTypeI) + 1;
    // if (type == 1) {
      var openingBankI = that.data.openingBankI;
      var bank_code = that.data.openingBank[openingBankI].bank_code;
      var bank_name = that.data.openingBank[openingBankI].bank_name;
    // } else {
    //   var bank_name = that.data.accountType[that.data.accountTypeI];
    // }
    app.ajax({
      url: "Partner/Bank/addBank",
      data: {
        // "type": type,
        "p_name": that.data.username,
        "bank_code": bank_code,
        "bank_name": bank_name,
        "p_sn": that.data.bankcard,
        "phone": that.data.phonenum,
        "id_card": that.data.idcard,
        "address": that.data.openingaddress
      },
      success: function (res) {
        if (res.data.code == 1000) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            const pages = getCurrentPages();
            const target=pages[pages.length-2];
            target.getBank();
            wx.navigateBack({
              delta: 1
            })
          }, 1000);

        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //修改银行信息
  editBank() {
    var that = this;
    var bank_id = that.data.bank_id;
    // var type = parseInt(that.data.accountTypeI) + 1;
    // if (type == 1) {
      var openingBankI = that.data.openingBankI;
      var bank_code = that.data.openingBank[openingBankI].bank_code;
      var bank_name = that.data.openingBank[openingBankI].bank_name;
    // } else {
    //   var bank_name = that.data.accountType[that.data.accountTypeI];
    // }
    app.ajax({
      url: "Partner/Bank/editBank",
      data: {
        "bank_id": bank_id,
        // "type": type,
        "p_name": that.data.username,
        "bank_code": bank_code,
        "bank_name": bank_name,
        "p_sn": that.data.bankcard,
        "phone": that.data.phonenum,
        "id_card": that.data.idcard,
        "address": that.data.openingaddress
      },
      success: function (res) {
        if (res.data.code == 1000) {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            const pages = getCurrentPages();
            const target=pages[pages.length-2];
            target.getBank();
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //查询单个银行信息
  findBank(id) {
    var that = this;
    app.ajax({
      url: "Partner/Bank/findBank",
      data: {
        "bank_id": id
      },
      success: function (res) {
        if (res.data.code == 1000) {
          // if (res.data.data.type == 1) {
            for (var k in that.data.openingBank) {
              if (that.data.openingBank[k].bank_name == res.data.data.bank_name) {
                that.data.openingBankI = k;
                that.setData({
                  openingBankI: k,
                })
                break;
              }
            }
            that.setData({
              username: res.data.data.p_name,
              bankcard: res.data.data.p_sn,
              phonenum: res.data.data.phone,
              idcard: res.data.data.id_card,
              openingaddress: res.data.data.address,
              agree: true
            })
          // } else if (res.data.data.type == 2) {
          //   that.setData({
          //     accountTypeI: 1,
          //     username: res.data.data.p_name,
          //     bankcard: res.data.data.p_sn,
          //     phonenum: res.data.data.phone,
          //     agree: true
          //   })
          // } else if (res.data.data.type == 3) {
          //   that.setData({
          //     accountTypeI: 2,
          //     username: res.data.data.p_name,
          //     bankcard: res.data.data.p_sn,
          //     phonenum: res.data.data.phone,
          //     agree: true
          //   })
          // }

        }
      }
    })
  },

})